package com.jar.exercise.java8新特性.Lambda表达式;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * @author ZhangShengHao
 * @className Demo1
 * @description TODO
 * @date 2020/1/27 20:01
 */
public class Demo {
	/**
	 * 测试1、无参数，无返回值
	 */
	@Test
	public void test1() {
		Runnable r = new Runnable() {
			@Override
			public void run() {
				System.out.println("我真帅");
			}
		};
		r.run();
		System.out.println("--------------------");
		Runnable s = () -> System.out.println("我真的很帅");
		s.run();
	}

	/**
	 * 测试2、有参数，有返回值
	 */
	@Test
	public void test2() {
		/*使用前*/
		Comparator<Integer> comparator = new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return Integer.compare(o1, o2);
			}
		};
		System.out.println(comparator.compare(24, 23));
		/*使用后*/
		System.out.println("---------------------------------");
		Comparator<Integer> comparator1 = (o1, o2) -> Integer.compare(o1, o2);
		System.out.println(comparator1.compare(24, 23));
		/*方法引用*/
		System.out.println("---------------------------------");
		Comparator<Integer> comparator2 = Integer::compare;
		System.out.println(comparator2.compare(24, 23));
	}

	/**
	 * 一个参数，没有返回值
	 */
	@Test
	public void test3() {
		Consumer<String> consumer = new Consumer<String>() {
			@Override
			public void accept(String s) {
				System.out.println(s + "你妹的");
			}
		};
		consumer.accept("你好呀");
		System.out.println("----------------------");
		Consumer<String> consumer1 = System.out::println;
		consumer1.accept("你好呀");
	}
}
